package swu.cx.finalproject

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Intent
import android.graphics.drawable.AnimationDrawable
import android.graphics.drawable.ClipDrawable
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.Contacts
import android.provider.Settings
import android.util.DisplayMetrics
import android.util.Log
import android.view.animation.BounceInterpolator
import android.widget.ImageView
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*
import kotlin.math.sin
class MainActivity : AppCompatActivity() {
    private var i=1
    val rowDistance = (sin((45.toDouble() * 3.1415926) / 180) * 340).toFloat()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        launchGif()
        start.setOnClickListener {
            showMenu()
        }
        menu1.setOnClickListener {
            Intent().apply {
                action=(Intent.ACTION_VIEW)
                data = Contacts.People.CONTENT_URI
                startActivity(this)
            }
        }
        menu5.setOnClickListener {
            startActivity(Intent(this,show_picture::class.java))
        }
        menu8.setOnClickListener {
            Intent().apply {
                action=Intent.ACTION_DIAL
                startActivity(this)
            }
        }
        menu2.setOnClickListener {
            Intent().apply {
                action="android.media.action.IMAGE_CAPTURE"
                startActivity(this)
            }
        }
        menu6.setOnClickListener {
            Intent().apply {
                action=Intent.ACTION_VIEW
                 data=Uri.parse("http://www.baidu.com")
                startActivity(this)
            }
        }
        menu7.setOnClickListener {
            Intent().apply {
                action=Settings.ACTION_BLUETOOTH_SETTINGS
                startActivity(this)
            }
        }
        menu3.setOnClickListener {
            startActivity(Intent(this,LoginActivity::class.java))
        }
        menu4.setOnClickListener {
            Intent().apply {
                action=Intent.ACTION_VIEW
                type="vnd.android-dir/mms-sms"
                startActivity(this)
            }
        }
    }
    override fun onResume() {
        super.onResume()
        peoms.isSelected=true
            val clipDrawable=team.drawable as ClipDrawable
            val timer=Timer()
            timer.schedule(object : TimerTask(){
                override fun run() {
                    clipDrawable.level+=100
                    if (clipDrawable.level==10000){
                        timer.cancel()
                    }
                }
            },0,100)
    }
    fun launchGif() {
        (imageGif.drawable as AnimationDrawable).start()
    }
    fun showMenu() {
        if (i==9){
            start.isClickable=false
            return
        }
            val scaleX = ObjectAnimator.ofFloat(start, "scaleX", 1.2f, 1.0f)
            val scaleY = ObjectAnimator.ofFloat(start, "scaleY", 1.2f, 1.0f)
            AnimatorSet().apply {
                duration = 500
                playTogether(scaleX, scaleY)
                start()
                addListener(object : newListener() {
                    override fun onAnimationEnd(p0: Animator?) {
                        val menu = Container.findViewWithTag<ImageView>("$i")
                        val tx=ObjectAnimator.ofFloat(menu,"translationX",getmoveX(i))
                        val ty=ObjectAnimator.ofFloat(menu,"translationY",getmoveY(i))
                        val rotate=ObjectAnimator.ofFloat(menu,"rotation",0f,360f)
                        AnimatorSet().apply {
                            playTogether(tx,ty,rotate)
                            interpolator=BounceInterpolator()
                            duration=1000
                            start()
                            addListener(object :newListener(){
                                override fun onAnimationEnd(p0: Animator?) {
                                       ObjectAnimator.ofFloat(start,"rotation",(45*i).toFloat()).apply {
                                           duration=200
                                           start()
                                           addListener(object : newListener() {
                                               override fun onAnimationEnd(p0: Animator?) {
                                                   i++
                                                   showMenu()
                                               }
                                           })
                                       }
                                }
                            })
                        }
                    }
                })
            }
    }
    fun getmoveX(i:Int)=when(i){
        1->0f
        2->rowDistance
        3->300f
        4->rowDistance
        5->0f
        6->-rowDistance
        7->-300f
        else->-rowDistance
    }
    fun getmoveY(i:Int)=when(i){
       1->-300f
       2->-rowDistance
       3->0f
       4->rowDistance
       5->300f
       6->rowDistance
       7->0f
       else->-rowDistance
    }
}
open class newListener:Animator.AnimatorListener{
    override fun onAnimationStart(p0: Animator?) {
    }

    override fun onAnimationEnd(p0: Animator?) {
    }

    override fun onAnimationCancel(p0: Animator?) {
    }

    override fun onAnimationRepeat(p0: Animator?) {
    }
}